package Ep06_BinaryTree.DFS.T3_0144;

import Ep06_BinaryTree.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/4/17 10:33
 * 说明：144 前序遍历  ——  迭代写法
 */

public class PreOrderTraversal_iteration {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            res.add(node.val);
            if (node.right != null){
                stack.push(node.right);
            }
            if (node.left != null){
                stack.push(node.left);
            }
        }
        return res;
    }
}
